package com.nguc.ngucpractice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nguc.ngucpractice.dao.StudentDao;
import com.nguc.ngucpractice.pojo.Student;
import com.nguc.ngucpractice.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User:曹帅
 * Date:2020/6/22
 * Version:1.0
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;


    public PageInfo<Student> findAll(int page, int pageSize) {
        PageHelper.startPage(1, 10);
        List<Student> list = studentDao.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String delStu(Integer sid) {
        Student student = new Student();
        student.setSid(sid);
        int count = studentDao.selectCount(student);
        if (count == 1) {
            studentDao.deleteByPrimaryKey(sid);
            System.out.printf("删除成功");
            return "删除成功";
        } else {
            System.out.printf("未查询到sid=" + sid + "的学生,删除失败");
            return "未查询到该学生,删除失败";
        }
    }

    @Override
    public String addStu(Student student) {
        studentDao.insert(student);
        return "新增成功";
    }

    @Override
    public Map<String, Object> queryStu(String name) {
        Student student = new Student();
        student.setName(name);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        int count = studentDao.selectCount(student);
        if (count > 1) {
            map.put("studentList", studentDao.select(student));
            list.add(map);
            System.out.printf("查找成功");
            return map;
        }
        if (count == 1) {
            map.put("studentList", studentDao.selectOne(student));
            list.add(map);
            System.out.printf("查找单个学生成功");
            return map;
        } else {
            System.out.printf("未查询到该学生");
            return map;
        }
    }

    @Override
    public String updateStu(Student student) {
        studentDao.updateByPrimaryKey(student);
        return "修改成功";
    }
}
